﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.Services;
using System.IO;
using System.Drawing;
//using Portal.SODC.IDIS;

/// <summary>
/// Summary description for MediaWs
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]

public class MediaWs : System.Web.Services.WebService
{
    //public class WsReturnParamCls
    //{
    //    public bool Error=false;
    //    public string ErrorMsg="";
    //}

    public MediaWs()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string CheckWs()
    {
        return "Ok";
    }

    string MediaPathRoot
    {
        get
        {
            //return Portal.Environment.Environments.WorkingPath + "\\";            
            return string.Empty;
        }
    }

    [WebMethod]
    public string CreateDirectory(string SecCode, string DirectoryName, string ClientRootPath)
    {
        //if (oWsReturnParamCls == null) oWsReturnParamCls = new WsReturnParamCls();
        try
        {
            DirectoryName = DirectoryName.Replace(ClientRootPath, "");
            DirectoryName = MediaPathRoot + DirectoryName;
            System.IO.Directory.CreateDirectory(DirectoryName);
            //oWsReturnParamCls.ErrorMsg = "Ok";
            return null;
        }
        catch (Exception ex)
        {
            return ex.Message.ToString();
            //oWsReturnParamCls.Error = true;
            //oWsReturnParamCls.ErrorMsg = ex.Message.ToString();
        }
    }


    [WebMethod]
    public string DeleteDirectory(
        string SecCode,
        string DirectoryName,
        string ClientRootPath)
    {
        //if (oWsReturnParamCls == null) oWsReturnParamCls = new WsReturnParamCls();
        try
        {
            DirectoryName = DirectoryName.Replace(ClientRootPath, "");
            DirectoryName = MediaPathRoot + DirectoryName;
            System.IO.Directory.Delete(DirectoryName, true);
            //oWsReturnParamCls.ErrorMsg = "Ok";
            return null;
        }
        catch (Exception ex)
        {
            return ex.Message.ToString();
            //oWsReturnParamCls.Error = true;
            //oWsReturnParamCls.ErrorMsg = ex.Message.ToString();
        }
    }

    public byte[] GetBytesFromImageFile(string ImageFile)
    {
        FileStream fs =
          new FileStream(ImageFile,
             FileMode.OpenOrCreate, FileAccess.Read);
        Byte[] img = new Byte[fs.Length];

        fs.Read(img, 0, Convert.ToInt32(fs.Length));
        fs.Close();

        return img;
    }

    [WebMethod]
    public byte[] DownloadFile(string SecCode, string FileName, string ClientRootPath)
    {
        try
        {
            FileName = FileName.Replace(ClientRootPath, "");
            FileName = MediaPathRoot + FileName;
            byte[] b = GetBytesFromImageFile(FileName);
            return b;
        }
        catch (Exception ex)
        {
            ex.Source = "";
            return null;
        }
    }

    [WebMethod]
    public bool CopyDirectory(string SecCode, string FromDir, string ToDir)
    {
        //return Ultility.CopyDirectory(FromDir, ToDir);
        return true;
    }

    [WebMethod]
    public string UploadFile(string SecCode, string FileName, byte[] b, string ClientRootPath)
    {
        try
        {
            //throw new Exception("running but not success!");

            FileName = FileName.Replace(ClientRootPath, "");
            FileName = MediaPathRoot + FileName;

            FileStream fs = System.IO.File.Create(FileName);
            fs.Read(b, 0, b.Length);
            //Bitmap a = new Bitmap(fs);
            //a.Save(FileName);
            fs.Write(b, 0, b.Length);
            fs.Close();
            return null;
        }
        catch (Exception ex)
        {
            return ex.Message.ToString();
        }
    }


    [WebMethod]
    public string DeleteFile(string SecCode, string FileName, string ClientRootPath)
    {
        try
        {
            FileName = FileName.Replace(ClientRootPath, "");
            FileName = MediaPathRoot + FileName;
            System.IO.File.Delete(FileName);
            return null;
        }
        catch (Exception ex)
        {
            return ex.Message.ToString();
        }
    }
}
